//
//  BaseNewsItemView.m
//  BaoVn
//
//  Created by Le Anh Tai on 11/8/14.
//  Copyright (c) 2014 Le Anh Tai. All rights reserved.
//

#import "BaseNewsItemView.h"
//#import "UIImageView+WebCache.h"

@implementation BaseNewsItemView

- (instancetype)init
{
    if (self = [super init])
    {
        [self initializeDefaultConfigs];
        [self initializeControls];
    }
    
    return self;
}

- (void)hideImage
{
    _hideImage = YES;
    
    self.frame = self.frame;
}

- (void)initializeDefaultConfigs
{
    _marginBottom = 0;
    _marginLeft = 8;
    _marginRight = 8;
    _marginTop = 8;
    _paddingImageAndContent = 8;
}

- (void)initializeControls
{
    _imageView = [[UIImageView alloc] init];
    _imageView.layer.cornerRadius = 4;
    _imageView.layer.borderColor = COLOR_LIGHT_GRAY.CGColor;
    _imageView.layer.borderWidth = 1;
//    _imageView.contentMode = UIViewContentModeScaleAspectFill;
    _imageView.clipsToBounds = YES;
    [self addSubview:_imageView];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.textColor = [UIColor blackColor];
    _titleLabel.font = FONT_BOLD(16);
    _titleLabel.numberOfLines = 0;
    _titleLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:_titleLabel];
    
    _descriptionLabel = [[UILabel alloc] init];
    _descriptionLabel.textColor = [UIColor grayColor];
    _descriptionLabel.font = FONT(12);
    _descriptionLabel.numberOfLines = 0;
    _descriptionLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:_descriptionLabel];
    
    _createdDateLabel = [[UILabel alloc] init];
    _createdDateLabel.textColor = [UIColor lightGrayColor];
    _createdDateLabel.font = FONT(11);
    _createdDateLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:_createdDateLabel];
}

- (void)loadData:(NSManagedObject *)data
{
    _data = data;
    
    NSString *imageUrl = [_data valueForKey:ICON_KEY];
    if (imageUrl)
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
            NSURL *url = [NSURL URLWithString:imageUrl];
            NSData *imageData = [NSData dataWithContentsOfURL:url];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                if (imageData)
                {
                    UIImage *image = [UIImage imageWithData:imageData];
                    self.imageView.image = image;
                }
                else
                {
                    self.imageView.image = nil;
                    
                    if ([_delegateLazyLoading respondsToSelector:@selector(newsItemView:didLoadImageWithError:)])
                    {
                        [_delegateLazyLoading newsItemView:self didLoadImageWithError:@"Image URL is nil"];
                    }
                }
            });
            
        });
    }
    else
    {
        self.imageView.image = nil;
        
        if ([_delegateLazyLoading respondsToSelector:@selector(newsItemView:didLoadImageWithError:)])
        {
            [_delegateLazyLoading newsItemView:self didLoadImageWithError:@"Image URL is nil"];
        }
    }
    
    //
    self.titleLabel.text = [_data valueForKey:TITLE_KEY];
    self.descriptionLabel.text = [_data valueForKey:DESCRIPTION_KEY];
    self.createdDateLabel.text = [_data valueForKey:CREATED_DATE_KEY];
}

@end
